<%@ page import="javax.security.auth.login.LoginContext"%>
<%@ page import="org.ow2.bonita.facade.QueryDefinitionAPI"%>
<%@ page import="org.ow2.bonita.light.LightProcessDefinition"%>
<%@page import="org.ow2.bonita.facade.def.majorElement.ProcessDefinition"%>
<%@ page import="org.ow2.bonita.facade.uuid.ProcessDefinitionUUID"%>
<%@ page import="org.ow2.bonita.util.AccessorUtil"%>
<%@ page import="org.ow2.bonita.util.SimpleCallbackHandler"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Collections"%>

<%
final Object isAdmin = request.getSession().getAttribute("isadmin");
if (isAdmin == null || !((Boolean) isAdmin).booleanValue()) {
  response.sendRedirect("home.jsp");
}

final String msg = request.getParameter("msg");

final int numberOfElementsToRetrive = 20;
List<LightProcessDefinition> journalProcesses = Collections.emptyList();
List<LightProcessDefinition> historyProcesses = Collections.emptyList();
try {
  final String username = (String) request.getSession().getAttribute("username");
  final LoginContext loginContext = new LoginContext("BonitaStore", new SimpleCallbackHandler(username, ""));
  loginContext.login();
  final QueryDefinitionAPI journalQueryDefinitionAPI = AccessorUtil.getQueryDefinitionAPI(AccessorUtil.QUERYLIST_JOURNAL_KEY);
  //lista dei casi archiviati e memorizzati automaticamente della storia del db
  final QueryDefinitionAPI historyQueryDefinitionAPI = AccessorUtil.getQueryDefinitionAPI(AccessorUtil.QUERYLIST_HISTORY_KEY);
  
  journalProcesses = journalQueryDefinitionAPI.getLightProcesses(0, numberOfElementsToRetrive);
  historyProcesses = historyQueryDefinitionAPI.getLightProcesses(0, numberOfElementsToRetrive);

  loginContext.logout();
} catch (Exception e) {
  e.printStackTrace();
  Throwable t = e;
  while (t.getCause() != null) {
    t = t.getCause();
  }
  final String errorMsg = "Error while listing processes: " + t.getMessage();
  response.sendRedirect("home.jsp?msg=" + errorMsg);  
}
%>


<jsp:include page="header.html"/>
<%if (msg != null) {%>
  <b><%=msg %></b><br><br>
<%}%>

<html>
<head>
<title> Manage Processes </title>
</head>
<body>
<h1> MANAGE PROCESSES</h1>

<a href="home.jsp">Home</a>&nbsp;&nbsp;&nbsp;
<a href="Instanze.jsp">Manage instances</a>&nbsp;&nbsp;&nbsp;
<a href="HomeBAM.jsp">Business Activity Monitoring</a>&nbsp;&nbsp;&nbsp;
<br><br>

<form name='deploy' method='get' action='actions/apiCall.jsp'><br>
Path to .bar file&nbsp;&nbsp;
  <input type='text' name='barFile'>
  <input type='hidden' name='back' value='processes'>
  <input type='hidden' name='action' value='deployProcess'>
  <input type='submit' name='deploy' value='Deploy' >
</form>
<br>
<br>

<TABLE BORDER="1" CELLPADDING="10" WIDTH="600px">
<CAPTION>List of journal processes (max: <%=numberOfElementsToRetrive %>)</CAPTION>
<TR>
  <TH>Process Name</TH>
  <TH>Version</TH>
  <TH>Actions</TH>
</TR>

<%
for (LightProcessDefinition process : journalProcesses) {
  final ProcessDefinitionUUID processUUID = process.getUUID();
  final ProcessDefinition.ProcessState state = process.getState();
  boolean isEnabled = true;
  if (ProcessDefinition.ProcessState.DISABLED.equals(state)) {
    isEnabled = false;
  }
%>
<TR>
<TD><%=process.getLabel() %></TD>
<TD align="center"><%=process.getVersion() %></TD>
<TD>
<% if (isEnabled) { %>
  <a href="actions/apiCall.jsp?action=createInstance&back=processes&processUUID=<%=processUUID %>">Create a new instance</a>&nbsp;&nbsp;&nbsp;
  <a href="actions/apiCall.jsp?action=disableProcess&back=processes&processUUID=<%=processUUID %>">Disable</a>&nbsp;&nbsp;&nbsp;
<%} else {%>
  <a href="actions/apiCall.jsp?action=enableProcess&back=processes&processUUID=<%=processUUID %>">Enable</a>&nbsp;&nbsp;&nbsp;
  <a href="actions/apiCall.jsp?action=archiveProcess&back=processes&processUUID=<%=processUUID %>">Archive</a>&nbsp;&nbsp;&nbsp;
<%}%>
  <a href="actions/apiCall.jsp?action=deleteProcess&back=processes&processUUID=<%=processUUID %>">Delete</a>&nbsp;&nbsp;&nbsp;
</TD>
</TR>  
<%}%>
</TABLE>
  
<br><br>
<TABLE BORDER="1" CELLPADDING="10" WIDTH="600px">
<CAPTION>List of history processes (max: <%=numberOfElementsToRetrive %>)</CAPTION>
<TR>
  <TH>Name</TH>
  <TH>Version</TH>
  <TH>Actions</TH>
</TR>

<%
for (LightProcessDefinition process : historyProcesses) {
  final ProcessDefinitionUUID processUUID = process.getUUID();
%>

<TR>
<TD><%=process.getLabel() %></TD>
<TD align="center"><%=process.getVersion() %></TD>
<TD>
<a href="actions/apiCall.jsp?action=deleteProcess&back=processes&processUUID=<%=processUUID %>">Delete</a>&nbsp;&nbsp;&nbsp;
</TD>
</TR>
<%}%>
</TABLE>
<br><br>
</body>
</html>
<jsp:include page="footer.html"/>